#include "archtypemodel.h"

ArchtypeModel::ArchtypeModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}

int ArchtypeModel::rowCount(const QModelIndex & parent) const{
    return m_archs.size();
}

int ArchtypeModel::columnCount(const QModelIndex & parent) const{
    return 1;
}


int ArchtypeModel::AddArchtype(const QString &name){
    bool change_name = false;

    for(int i = 0; i < m_archs.size(); i++){
        if(m_archs[i]->GetName()==name){
            change_name = true;
        }
    }

    QString newname;
    if(change_name){
        int i=2;
        bool end=false;
        while(!end){
            bool found = false;

            for(int j = 0; j < m_archs.size(); j++){
                if(m_archs[j]->GetName()==name+QString::number(i)){
                    found=true;
                }
            }
            if(found)
                i++;
            else
                end=true;
        }
        newname = name+QString::number(i);

    }else{
        newname = name;
    }


    AED_Archtype *arch = new AED_Archtype(newname);
    m_archs.append(arch);
    emit layoutChanged();
    return 1;
}

void ArchtypeModel::CopyArchtype(const AED_Archtype *arch){
    AED_Archtype *newarch = new AED_Archtype(*arch);

    QString name = newarch->GetName();
    int i=2;
    bool end=false;
    while(!end){
        bool found = false;

        for(int j = 0; j < m_archs.size(); j++){
            if(m_archs[j]->GetName()==name+QString::number(i)){
                found=true;
            }
        }

        if(found)
            i++;
        else
            end=true;
    }


    newarch->SetName(newarch->GetName()+QString::number(i));
    m_archs.append(newarch);
    emit layoutChanged();
}

void ArchtypeModel::EraseArchtype(int index){

    delete m_archs[index];
    m_archs.erase(m_archs.begin()+index);
    emit layoutChanged();
    emit erased();
}

void ArchtypeModel::Clean(){
    for(int i = 0; i < m_archs.size(); i++)
        delete m_archs[i];

    m_archs.clear();
    emit layoutChanged();
}

/*void ArchtypeModel::UpdateArchtype(const AED_Archtype &arch, int index){
    m_archs[index] = arch;
}*/

QVariant ArchtypeModel::data ( const QModelIndex & index, int role) const{
    if(role==Qt::DisplayRole){
        int row = index.row();

        QVariant name = m_archs[row]->GetName();
        return name;
    }
    return QVariant(QVariant::Invalid);
}

QVariant ArchtypeModel::headerData ( int section, Qt::Orientation orientation, int role) const{
    if(orientation==Qt::Horizontal){
        if (role==Qt::DisplayRole){
            return tr("Archtype Name");
        }else{
            return QVariant(QVariant::Invalid);
        }
    }else{
        if (role==Qt::DisplayRole){
            QVariant h(QVariant::Int);
            h=section+1;
            return h;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }
}

AED_Archtype* ArchtypeModel::GetArchtype(int index){
    return m_archs[index];
}

AED_Archtype* ArchtypeModel::GetArchtype(QString &name){
    for(int i=0;i<m_archs.count();i++){
        if(m_archs[i]->GetName()==name)
            return m_archs[i];
    }

    return 0;
}

void ArchtypeModel::Save(QTextStream &file){
    for(int i=0;i<m_archs.size();i++){
        m_archs[i]->Save(file);
    }
}

void ArchtypeModel::Save(QDomDocument &doc, QDomElement &header){
    for(int i=0;i<m_archs.size();i++){
        m_archs[i]->Save(doc,header);
    }
}

bool ArchtypeModel::Load(QDomElement &header, ImageManager &imgmngr, ScriptModel &scrmdl){
    QDomNodeList archtypes = header.elementsByTagName("archtype");
    for(int i=0;i<archtypes.count();i++){
        QDomElement aelem = archtypes.at(i).toElement();
        AED_Archtype *arch = new AED_Archtype(aelem.attribute("name"));
        arch->Load(aelem, imgmngr,scrmdl);
        m_archs.append(arch);
    }
    emit layoutChanged();
    return true;
}

int ArchtypeModel::FindArchtype(AED_Archtype *arch){
    for(int i=0;i<m_archs.size();i++){
        if(m_archs[i] == arch)
            return i;
    }

    return -1;
}
